#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
| Created on 2020/3/27 9:59

| @author: aimee.mei
"""

import sys, logging,traceback
from util.db_util import execute_query
from  proj_spec.industrial_chain.change_time import date_change_string,timestamp_change_date
import pandas as pd
import datetime

def get_client_last_updated_time(param,tablename):
    '''
    获取客户产品最后更新时间

    :param param: 查询的字段名
    :param tablename: 查询的表名

    :return: updated_time
    '''
    try:
        results = execute_query(
            "select max(%s) from %s  where tree_id = 132" % (param,tablename))

        result = date_change_string(results[0][0])
        return result
    except Exception as e:
        logging.exception("Retrieve latest id failed!")
        logging.error(e)
        traceback.print_exc()
        return None


def get_client_last_update_time(tablename):
    '''
    获取update_time时间

    :param tablename: 表名

    :return: update_time时间
    '''
    try:
        results = execute_query(
            "select max(update_time) from %s " % (tablename))

        result = results[0][0]
        return result
    except Exception as e:
        logging.exception("Retrieve latest id failed!")
        logging.error(e)
        traceback.print_exc()
        return None


def get_platform_last_updated_time():
    '''
    获取数库产品最后更新时间
    | dict_industry表和dict_product_rs的update_time字段比较
    | 取两表较大的时间为数库产品最后更新时间

    :return: 数库产品最后更新时间
    '''
    try:

        results1 = get_client_last_update_time("sam_platform.dict_industry")
        results2 = get_client_last_update_time("sam_platform.dict_product_rs")

        if results1 > results2:
            results = results1
        else:
            results = results2

        result = date_change_string(results)

        return result
    except Exception as e:
        logging.exception("Retrieve latest id failed!")
        logging.error(e)
        traceback.print_exc()
        return None

#